package com.itheima.stock.config;

import com.itheima.stock.pojo.vo.StockInfoConfig;
import com.itheima.stock.utils.IdWorker;
import com.itheima.stock.utils.ParserStockInfoUtil;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 定义公共配置类
 */
@Configuration
@EnableConfigurationProperties({StockInfoConfig.class})//开启对象相关配置对象的加载 yml文件里面的
public class CommonConfig {

    /**
     * 雪花算法
     * @return
     */
    @Bean
    public IdWorker idWorker(){

        /**
         * 参数1机器id
         * 参数2机房id
         * 机房和机器编号一般由运维人员进行唯一性规划
         */
        return new IdWorker(1l,2l);
    }

    /**
     * 定义解析股票大盘外盘个股板块相关信息的工具数bean
     * @return
     * @param
     * @return
     */
//    @Bean
//    public ParserStockInfoUtil parserStockInfoUtil(IdWorker idWorker){
//   return   new ParserStockInfoUtil(idWorker);
//    }
    @Bean
    public ParserStockInfoUtil parserStockInfoUtil(){
        return   new ParserStockInfoUtil(idWorker());
    }

    /**
     * 定义主题 交换机
     * durable：消息持久化
     */
    @Bean
    public TopicExchange stockTopicExchange(){
        return new TopicExchange("stockExchange",true,false);
    }

    /**
     * 定义队列
     */
    @Bean
    public Queue innerMarketQueue(){
        return new Queue("innerMarketQueue",true);//大盘
    }

    /**
     * 绑定国内大盘队列到股票主题交换机
     */
    @Bean
    public Binding bindingInnerMarketQueue(){
      return   BindingBuilder.bind(innerMarketQueue()).to(stockTopicExchange()).with("inner.market");
    }
}
